package com.hotel.project.demo.controller;

import com.hotel.project.demo.domain.HotelPictureEntity;
import com.hotel.project.demo.domain.ResponseBean;
import com.hotel.project.demo.service.HotelPictureService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/hotel/picture")
public class HotelPictureController extends BaseController<HotelPictureService, HotelPictureEntity>{
    /**
     * 根据图片标题储存图片地址
     * @param hotelId
     * @param hotelName
     * @param title
     * @param imageUrl
     * @return
     */
    @PostMapping("/save-picture")
    public ResponseBean saveHotelImages(@PathVariable Long hotelId,
                                        @PathVariable String hotelName,
                                        @PathVariable String title,
                                        @PathVariable String[] imageUrl){
        ResponseBean rb = new ResponseBean();
        try {
            for (int i = 0; i < imageUrl.length - 1; i++) {
                HotelPictureEntity e = new HotelPictureEntity();
                e.setHotelId(hotelId);
                e.setHotelName(hotelName);
                e.setTitle(title);
                e.setType(title);
                e.setUrl(imageUrl[i]);
                if (!service.save(e)){
                    rb = errorEdit(rb,10001l);
                    return rb;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return  rb;
    }

    /**
     * 根据id或标题删除图片
     * @param id
     * @param title
     * @return
     */
    @DeleteMapping("/delete-picture")
    public ResponseBean deleteHotelImage(@PathVariable Long id,
                                         @PathVariable String title){
        ResponseBean rb = new ResponseBean();
        try {
            if (id == null){
                id = 0l;
            }
            if (title == null){
                title = "";
            }
            if (!service.hotelPictureDelete(id, title)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }
}
